<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>进货</title>
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="./images/favicon.png" />

    <!-- <link href="./css/style.css" rel="stylesheet" /> -->

    <link href="assets/css/lib/font-awesome.min.css" rel="stylesheet">
    <link href="assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="assets/css/lib/jsgrid/jsgrid-theme.min.css" rel="stylesheet" />
    <link href="assets/css/lib/jsgrid/jsgrid.min.css" type="text/css" rel="stylesheet" />
    <link href="assets/css/lib/menubar/sidebar.css" rel="stylesheet">
    <link href="assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/lib/helper.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">

    <!-- jquery vendor -->
    <script src="assets/js/lib/jquery.min.js"></script>
    <script src="assets/js/lib/jquery.nanoscroller.min.js"></script>


    <script>
        function doAddSaleCommodityFunction() {
            var inputBarCode = document.getElementById("barCode").value;
            var hasUpdated = false;
            window.db.clients.forEach(function (item, index, array) {
                if (item["barCode"] == inputBarCode) {
                    item["amount"] = item["amount"] + 1;
                    var sumPrice = item["amount"] * item["nowSellPrice"];
                    item["sumPrice"] = sumPrice.toFixed(2);
                    hasUpdated = true;
                    $("#jsGrid").jsGrid("loadData");
                    updateTotalPrice();
                    return;
                }
            })

            if (hasUpdated)
                return;

            var requestObj = {};
            requestObj["requestService"] = "QueryCommodityInfo";
            requestObj["requestMethod"] = "";
            var data = {};
            data["barCode"] = document.getElementById("barCode").value;
            requestObj["data"] = data;
            var requestJson = JSON.stringify(requestObj);

            var oneSellInfo = {};
            var sellingValidate = true;
            $.ajax({
                type: "POST",
                url: "http://192.168.70.135:80/api/",
                contentType: "application/json",
                data: requestJson,
                async: false,
                success: function (msg) {
                    oneSellInfo["barCode"] = msg.data.barCode;
                    oneSellInfo["productName"] = msg.data.productName;
                    
                    //查询上架状态
                    var requestObj = {};
                    requestObj["requestService"] = "DoSaleService";
                    requestObj["requestMethod"] = "checkSelling";
                    var data = {};
                    data["barCode"] = msg.data.barCode;
                    requestObj["data"] = data;
                    var requestJson = JSON.stringify(requestObj);
                    $.ajax({
                        type: "POST",
                        url: "http://192.168.70.135:80/api/",
                        contentType: "application/json",
                        data: requestJson,
                        async: false,
                        success: function (msg) {
                            if (!msg.data.result)
                                sellingValidate = false;
                        }
                    });
                }
            });

            if (!sellingValidate) {
                alert("商品 " + oneSellInfo["productName"] + " 未上架，无法销售！");
                return;
            }

            // 查询商品价格
            var requestObj = {};
            requestObj["requestService"] = "CommoditySellPriceService";
            requestObj["requestMethod"] = "queryCommoditySellPriceByBarcode";
            var data = {};
            data["barCode"] = document.getElementById("barCode").value;
            requestObj["data"] = data;
            var requestJson = JSON.stringify(requestObj);
            $.ajax({
                type: "POST",
                url: "http://192.168.70.135:80/api/",
                contentType: "application/json",
                data: requestJson,
                async: false,
                success: function (msg) {
                    oneSellInfo["defaultSellPrice"] = msg.data.defaultSellPrice;
                    if (msg.data.nowSellPrice == null || msg.data.nowSellPrice.length == 0) {
                        oneSellInfo["nowSellPrice"] = msg.data.defaultSellPrice
                    }
                    else {
                        oneSellInfo["nowSellPrice"] = msg.data.nowSellPrice
                    }
                }
            });
            oneSellInfo["amount"] = 1;
            var sumPrice = oneSellInfo["amount"] * oneSellInfo["nowSellPrice"]
            oneSellInfo["sumPrice"] = sumPrice.toFixed(2);
            window.db.clients.push(oneSellInfo);
            $("#jsGrid").jsGrid("loadData");
            updateTotalPrice();
        }
    </script>

    <script>
        $(document).ready(function () {
            $("button#doAddSaleCommodity").click(function () {
                doAddSaleCommodityFunction();
            })
        })
    </script>

    <script>
        function updateTotalPrice() {
            var totalPrice = 0;
            window.db.clients.forEach(function (item, index, array) {
                var sumPrice = Number(item["sumPrice"]);
                totalPrice = totalPrice + sumPrice;
            });
            totalPrice = '￥' + totalPrice.toFixed(2);
            $('#totalPrice').html(totalPrice);
        }
    </script>

    <script>
        $(document).ready(function () {
            $("#saleListSubmit").click(function () {
                var requestObj = {};
                requestObj["requestService"] = "DoSaleService";
                requestObj["requestMethod"] = "doOneSale";
                requestObj["data"] = window.db.clients;
                var requestJson = JSON.stringify(requestObj)
                $.ajax({
                    type: "POST",
                    url: "http://192.168.70.135:80/api/",
                    contentType: "application/json",
                    data: requestJson,
                    success: function (msg) {
                        alert("已成功执行销售请求");
                        location.reload();
                    }
                });
            })
        })
    </script>

    <script>
        $(document).ready(function () {
            $("#barCode").bind('keydown', function (event) {
                if (event.keyCode == "13") {
                    doAddSaleCommodityFunction();
                    $("#barCode").val(null);
                }
            });
        })
    </script>
</head>

<body>

    <script src="./common.js"></script>

    <div class="content-wrap">
        <div class="main">
            <div class="container-fluid">
                <div id="main-content">
                    <div class="row">
                        <div class="col-lg-6">
                            <div class="card">
                                <div class="card-header">
                                    <div class="card-title">
                                        <h4>购物清单 </h4>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="jsgrid-table-panel">
                                        <div id="jsGrid"></div>
                                    </div>
                                    </br>
                                    <button type="button" class="btn btn-primary m-b-10 m-l-5"
                                        id="saleListSubmit">结账</button>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6">
                            <div class="card">
                                <div class="card-header">
                                    <div class="card-title">
                                        <h4>下一件商品 </h4>
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="basic-form">
                                        <div class="row">
                                            <div class="col-lg-4">
                                                <div class="form-group">
                                                    <label>商品条码</label>
                                                    <input id="barCode" type="text" class="form-control">
                                                    <br />
                                                    <button type="button" class="btn btn-info m-b-10 m-l-5"
                                                        id="doAddSaleCommodity">确定</button>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-lg-4">
                                                <div class="form-group">
                                                    <label>价格总计</label>
                                                    <h1 id="totalPrice">￥0</h1>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="footer">
                                <div class="copyright">
                                    <p>Copyright © Designed &amp; Developed by <a href="#" target="_blank">Quixkit</a>
                                        2019</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- nano scroller -->
    <script src="assets/js/lib/menubar/sidebar.js"></script>
    <script src="assets/js/lib/preloader/pace.min.js"></script>
    <!-- sidebar -->

    <!-- JS Grid Scripts Start-->
    <script>
            (function () {

                var db = {

                    loadData: function (filter) {
                        return this.clients;
                    },

                    insertItem: function (insertingClient) {
                        this.clients.push(insertingClient);
                    },

                    updateItem: function (updatingClient) { },

                    deleteItem: function (deletingClient) {
                        var clientIndex = $.inArray(deletingClient, this.clients);
                        this.clients.splice(clientIndex, 1);
                    }

                };

                window.db = db;

                db.clients = [
                ];

            }());
    </script>
    <script src="assets/js/lib/jsgrid/jsgrid.core.js"></script>
    <script src="assets/js/lib/jsgrid/jsgrid.load-indicator.js"></script>
    <script src="assets/js/lib/jsgrid/jsgrid.load-strategies.js"></script>
    <script src="assets/js/lib/jsgrid/jsgrid.sort-strategies.js"></script>
    <script src="assets/js/lib/jsgrid/jsgrid.field.js"></script>
    <script src="assets/js/lib/jsgrid/fields/jsgrid.field.text.js"></script>
    <script src="assets/js/lib/jsgrid/fields/jsgrid.field.number.js"></script>
    <script src="assets/js/lib/jsgrid/fields/jsgrid.field.select.js"></script>
    <script src="assets/js/lib/jsgrid/fields/jsgrid.field.checkbox.js"></script>
    <script src="assets/js/lib/jsgrid/fields/jsgrid.field.control.js"></script>
    <script>
        $(function () {
            $("#jsGrid").jsGrid({
                height: "100%",
                width: "100%",
                filtering: false,
                editing: false,
                inserting: false,
                sorting: true,
                paging: true,
                autoload: true,
                pageSize: 15,
                pageButtonCount: 4,
                deleteConfirm: "Do you really want to delete the client?",
                controller: db,
                noDataContent: "目前是空的......",
                fields: [
                    { name: "barCode", title: "商品条码", type: "text", width: 100 },
                    { name: "productName", title: "商品名称", type: "text", width: 120 },
                    { name: "amount", title: "数量", type: "number", width: 30 },
                    { name: "defaultSellPrice", title: "原价", type: "number", align: "right", width: 30 },
                    { name: "nowSellPrice", title: "单价", type: "number", align: "right", width: 30 },
                    { name: "sumPrice", title: "合计", type: "text", align: "right", width: 30 },
                    { type: "control" }
                ]
            });
        });
    </script>
    <!-- JS Grid Scripts End-->

</body>

</html>